package com.highspeed.bdk.controller.physical.service;

import com.highspeed.bdk.TestMainApplication;
import com.highspeed.bdk.bean.entity.physical.*;
import org.dom4j.DocumentException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {TestMainApplication.class})
@Transactional
public class PhysicalTopologyServiceTester extends BaseDeviceServiceTester {
    private PhysicalTopology physicalTopology;

    @Before
    public void init() {
        PhysicalTopology pt = createPhysicalTopology();

        for (int a = 0; a < 5; a++) {
            MachineBox mb = createMachineBox();
            for (int i = 0; i < 5; i++) {
                Board board = createBoard();

                for (int j = 0; j < 5; j++) {
                    Cpu cpu = createCpu();
                    board.getCpus().add(cpu);
                }
                mb.getBoards().add(board);
            }
            pt.getMachineBoxes().add(mb);
        }

        this.physicalTopology = physicalTopologyService.save(pt);
        for (MachineBox machineBox : this.physicalTopology.getMachineBoxes()) {
            for (Board board : machineBox.getBoards()) {
                createPhysicalLink(board.getCpus().get(0), board.getCpus().get(1));
            }
        }
        this.physicalTopology = physicalTopologyService.save(physicalTopology);
        physicalTopologyService.flush();
        physicalTopologyService.getRepository().clear();
    }

    @Test
    public void update() {
        this.physicalTopology.getMachineBoxes().remove(0);
        physicalTopologyService.save(this.physicalTopology);
        physicalTopologyService.flush();
        List<PhysicalLink> links = physicalLinkService.findByPhysicalTopologyId(physicalTopology.getId());
        Assert.assertEquals(links.size(), 20);
    }


    @Test
    public void save() {
        Assert.assertNotNull(physicalTopology);
        physicalTopology = physicalTopologyService.findOne(physicalTopology.getId());
        Map<String, Object> params1 = new HashMap<String, Object>();
        params1.put("board.machineBox.physicalTopology.id", physicalTopology.getId());
        Page<Cpu> cpus = cpuService.query(params1, null);
        Assert.assertEquals(cpus.getContent().size(), 125);

        Map<String, Object> params2 = new HashMap<String, Object>();
        params2.put("machineBox.physicalTopology.id", physicalTopology.getId());
        Page<Board> boards = boardService.query(params2, null);
        Assert.assertEquals(boards.getContent().size(), 25);

        Map<String, Object> params3 = new HashMap<String, Object>();
        params3.put("physicalTopology.id", physicalTopology.getId());
        Page<MachineBox> machineBoxes = machineBoxService.query(params3, null);
        Assert.assertEquals(machineBoxes.getContent().size(), 5);
    }

    @Test
    public void deleteLinks() {

        List<PhysicalLink> oldLinks = physicalLinkService.findByPhysicalTopologyId(physicalTopology.getId());
        physicalLinkService.deleteLinksByBoardId(this.physicalTopology.getMachineBoxes().get(0).getBoards().get(0).getId());
        List<PhysicalLink> nowLinks = physicalLinkService.findByPhysicalTopologyId(physicalTopology.getId());
        Assert.assertEquals(oldLinks.size() - nowLinks.size(), 1);


        physicalLinkService.deleteLinksByMachineBoxId(this.physicalTopology.getMachineBoxes().get(0).getId());
        nowLinks = physicalLinkService.findByPhysicalTopologyId(physicalTopology.getId());
        Assert.assertEquals(oldLinks.size() - nowLinks.size(), 5);
    }

    @Test
    public void delete() {
        physicalTopologyService.delete(this.physicalTopology.getId());
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id_eq", physicalTopology.getId());
        params.put("enabled_eq", true);
        List logicTopology = physicalTopologyService.queryToList(params, null);
        Assert.assertTrue(logicTopology.isEmpty());
    }


    @Test
    public void deleteDeviceAndLinks() {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("board.machineBox.physicalTopology.id", physicalTopology.getId());
        Page<Cpu> linkCpus = cpuService.query(params, null);
        Assert.assertEquals(linkCpus.getContent().size(), 125);
        cpuService.delete(physicalTopology.getMachineBoxes().get(0).getBoards().get(0).getCpus().get(0).getId());
        cpuService.flush();
        Page<Cpu> nowLinkCpus = cpuService.query(params, null);
        Assert.assertEquals(linkCpus.getContent().size() - nowLinkCpus.getContent().size(), 1);

        List<PhysicalLink> mbLinks = physicalLinkService.findByPhysicalTopologyId(physicalTopology.getId());
        Assert.assertEquals(mbLinks.size(), 24);


        params = new HashMap<String, Object>();
        params.put("machineBox.physicalTopology.id", physicalTopology.getId());
        Page<Board> linkBoards = boardService.query(params, null);
        Assert.assertEquals(linkBoards.getContent().size(), 25);
        boardService.delete(physicalTopology.getMachineBoxes().get(1).getBoards().get(0).getId());
        Page<Board> nowLinkBoards = boardService.query(params, null);
        Assert.assertEquals(linkBoards.getContent().size() - nowLinkBoards.getContent().size(), 1);

        mbLinks = physicalLinkService.findByPhysicalTopologyId(physicalTopology.getId());
        Assert.assertEquals(mbLinks.size(), 23);

        params = new HashMap<String, Object>();
        params.put("physicalTopology.id", physicalTopology.getId());
        Page<MachineBox> linkMachineBoxes = machineBoxService.query(params, null);
        Assert.assertEquals(linkMachineBoxes.getContent().size(), 5);
        machineBoxService.delete(physicalTopology.getMachineBoxes().get(2).getId());
        Page<MachineBox> nowLinkMachineBoxes = machineBoxService.query(params, null);
        Assert.assertEquals(linkBoards.getContent().size() - nowLinkBoards.getContent().size(), 1);

        mbLinks = physicalLinkService.findByPhysicalTopologyId(physicalTopology.getId());
        Assert.assertEquals(mbLinks.size(), 18);
    }

    @Test
    public void query() {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", physicalTopology.getId());
        List<PhysicalTopology> list = physicalTopologyService.queryToList(params, null);
        Assert.assertEquals(list.size(), 1);
    }

    @Test
    public void parseXml() {
//        File file = new File("C:\\Users\\chiliao\\Desktop\\test1(1).xml");
//        try {
//            physicalTopologyService.importXml(file);
//        } catch (DocumentException e) {
//            e.printStackTrace();
//        }
    }

    @Test
    public void getById() {
        Integer id = 1;
        PhysicalTopology physicalTopology = physicalTopologyService.findOne(id);
        System.out.println(physicalTopology);
    }

    @Test
    public void downloadXml() {
        Integer id = 1;
        String str = physicalTopologyService.exportPhysicalToplical(id);
        System.out.println(str);
    }
}
